package com.cfh.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.util.Assert;

import com.cfh.domain.User;
import com.cfh.domain.UserExample;
import com.cfh.domain.UserExample.Criteria;
import com.cfh.persistence.dao.UserDAO;

public class UserServiceImpl implements UserService {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(UserServiceImpl.class);

	private UserDAO userDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cfh.service.UserService#getUserByUsernameAndPassword(java.lang.String
	 * , java.lang.String)
	 */
	public User getUserByUsernameAndPassword(String username, String password) {
		if (logger.isDebugEnabled()) {
			logger.debug("getUserByUsernameAndPassword(String, String) - start"); //$NON-NLS-1$
		}

		Assert.hasText(username);
		Assert.hasText(password);
		UserExample ue = new UserExample();
		Criteria ct = ue.createCriteria();
		ct.andLOGIN_NAMEEqualTo(username);
		ct.andPASSWORDEqualTo(password);
		List list = getUserDAO().selectByExample(ue);
		if (list != null && list.size() > 0) {
			User returnUser = (User) list.get(0);
			if (logger.isDebugEnabled()) {
				logger.debug("getUserByUsernameAndPassword(String, String) - end"); //$NON-NLS-1$
			}
			return returnUser;
		} else {
			return null;
		}
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void changePassword(User currentUser, String newPassword) {
		currentUser.setPASSWORD(newPassword);
		userDAO.updateByPrimaryKey(currentUser);
		
	}
	public void insert(User user) {
		userDAO.insert(user);
		
	}

	public List selectByExample(UserExample userExample, int start, int limit) {
		return userDAO.selectByExample(userExample, start, limit);
	}
	
	public int countByExample(UserExample example) {
		return userDAO.countByExample(example);
		
	}

	public int update(User user) {
		return userDAO.updateByPrimaryKeySelective(user);
	}

	public int deleteByExample(UserExample example) {
		return userDAO.deleteByExample(example);
	}

}
